package uit.shoeshop.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
import uit.shoeshop.dto.InformationDTO;
import uit.shoeshop.lib.Util;
import com.opensymphony.xwork2.ActionSupport;

import uit.shoeshop.model.AboutusModel;
import uit.shoeshop.dto.InformationDTO;
public class ManageaboutusAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private InformationDTO aboutus;
	
	private String title;
	private String content;
	
	private String link;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String execute() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = NONE;
    	AboutusModel about = new AboutusModel();
    	setLink(this.request.getRequestURL().toString());
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST") && getContent() != null && getTitle() != null){
        		if(about.updatePageAboutUs(getTitle(), getContent()) == 1)
        			return SUCCESS;
        		else
        			return ERROR;
        	}     		
    		setAboutus(about.getPageAboutUs());
    		if(getAboutus() == null || getAboutus().getTitle() == null){
    			ret= ERROR;
    		}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		about = null;
    	}    	
    	return ret;
    }

	/**
	 * @param aboutus the aboutus to set
	 */
	public void setAboutus(InformationDTO aboutus) {
		this.aboutus = aboutus;
	}

	/**
	 * @return the aboutus
	 */
	public InformationDTO getAboutus() {
		return aboutus;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param link the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}	
}
